package dao

import (
	"time"

	"github.com/didi/gendry/scanner"
)

//UserNav user_nav struct
type UserNav struct {
	Id                 int    `gorm:"id;pk" json:"id" default:""`
	Title              string `gorm:"title" json:"title" default:""`
	Key                string `gorm:"key" json:"key" default:""`
	Name               string `gorm:"name" json:"name" default:""`
	Component          string `gorm:"component" json:"component" default:""`
	Redirect           string `gorm:"redirect" json:"redirect" default:""`
	Icon               string `gorm:"icon" json:"icon" default:""`
	KeepAlive          int    `gorm:"keepAlive" json:"keepAlive" default:"1"`
	HideChildrenInMenu int    `gorm:"hideChildrenInMenu" json:"hideChildrenInMenu" default:"0"`
	Hidden             int    `gorm:"hidden" json:"hidden" default:"0"`
	Level              int    `gorm:"level;pk" json:"level" default:"1"`
	Sort               int    `gorm:"sort" json:"sort" default:"0"`
	ParentId           int    `gorm:"parent_id" json:"parent_id" default:"0"`
	// other
	UserNavAuth []UserNavAuth `json:"user_nav_auth"`
	UserNav     []UserNav     `json:"nav"`
}

// TableName sets the insert table name for this struct type
func (b UserNav) TableName() string {
	return "user_nav"
}

// UserNavs get user_nav list
func UserNavs(qs *Qs, other Other) (data []UserNav, err error) {
	wh, params := qs.Format()
	others := other.Format()
	rows, err := Query(`
		select a.* 
		from user_nav a 
		`+wh+others, params...)
	if err != nil {
		return
	}
	err = scanner.ScanClose(rows, &data)
	return
}

// UserNav get user_nav
func UserNavo(qs *Qs) (data UserNav, err error) {
	datas, err := UserNavs(qs, DefaultOther)
	if err != nil {
		return
	}
	if len(datas) == 0 {
		err = NotFound
		return
	}
	data = datas[0]
	return
}

//UserNavAuth user_nav_auth struct
type UserNavAuth struct {
	Id        int    `gorm:"id;pk" json:"id" default:""`
	UserNavId int    `gorm:"user_nav_id;pk" json:"user_nav_id" default:"0"`
	Name      string `gorm:"name" json:"name" default:""`
	Action    string `gorm:"action" json:"action" default:""`
	// other
	PermissionId string   `json:"permissionId"`
	ActionList   []string `json:"actionList"`
}

// TableName sets the insert table name for this struct type
func (b UserNavAuth) TableName() string {
	return "user_nav_auth"
}

// UserNavAuths get user_nav_auth list
func UserNavAuths(qs *Qs, other Other) (data []UserNavAuth, err error) {
	wh, params := qs.Format()
	others := other.Format()
	rows, err := Query(`
		select a.*, b.name as permissionId 
		from user_nav_auth a 
		left join user_nav b on a.user_nav_id=b.id 
		`+wh+others, params...)
	if err != nil {
		return
	}
	err = scanner.ScanClose(rows, &data)
	return
}

// UserNavAuth get user_nav_auth
func UserNavAutho(qs *Qs) (data UserNavAuth, err error) {
	datas, err := UserNavAuths(qs, DefaultOther)
	if err != nil {
		return
	}
	if len(datas) == 0 {
		err = NotFound
		return
	}
	data = datas[0]
	return
}

//UserRole user_role struct
type UserRole struct {
	Id         int       `gorm:"id;pk" json:"id" default:""`
	IsDelete   int       `gorm:"is_delete" json:"is_delete" default:"0"`
	CreateTime time.Time `gorm:"create_time" json:"create_time" default:""`
	ModifyTime time.Time `gorm:"modify_time" json:"modify_time" default:""`
	Navs       string    `gorm:"navs" json:"navs" default:""`
	Auths      string    `gorm:"auths" json:"auths" default:""`
	Name       string    `gorm:"name" json:"name" default:""`
}

// TableName sets the insert table name for this struct type
func (b UserRole) TableName() string {
	return "user_role"
}

// UserRoles get user_role list
func UserRoles(qs *Qs, other Other) (data []UserRole, err error) {
	wh, params := qs.Format()
	others := other.Format()
	rows, err := Query(`
		select a.* 
		from user_role a 
		`+wh+others, params...)
	if err != nil {
		return
	}
	err = scanner.ScanClose(rows, &data)
	return
}

// UserRole get user_role
func UserRoleo(qs *Qs) (data UserRole, err error) {
	datas, err := UserRoles(qs, DefaultOther)
	if err != nil {
		return
	}
	if len(datas) == 0 {
		err = NotFound
		return
	}
	data = datas[0]
	return
}
